<template>
  <el-dialog v-model="visibleModal" title="文章审批" width="500">
    <el-form
      ref="modelForm"
      style="max-width: 600px"
      :model="formData"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
      status-icon
    >
      <el-form-item label="审批意见" prop="approvalReason">
        <el-input
          v-model="formData.approvalReason"
          :rows="4"
          type="textarea"
          placeholder="请输入审批意见"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCancel">返回</el-button>
        <el-button type="primary" @click="handleCheck(1)">通过</el-button>
        <el-button type="warning" @click="handleCheck(2)">冻结</el-button>
        <el-button type="danger" @click="handleCheck(3)">驳回</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="checkModal">
import * as api from "@/api/index";
import type { FormInstance } from "element-plus";
import { ElMessage as message } from "element-plus";
import { ref } from "vue";
let emit = defineEmits(["refresh"]);
let rules = {
  approvalReason: [{ required: true, message: "审批意见不能为空！" }],
};
// form表单实例
let modelForm = ref<any>();
// 弹窗控制器
let visibleModal = ref(false);
// 单钱数据id
let id = ref("");
// 表单数据
let formData = ref({
  // 审批意见 1 通过 2 冻结 3 驳回
  approvalOpinion: 0,
  // 审批理由
  approvalReason: "",
});
let handleShow = (params: any) => {
  visibleModal.value = params.visible;
  id.value = params.id;
};

let handleCancel = () => {
  visibleModal.value = false;
  modelForm.value.resetFields();
};
// 通过
let handleCheck = (coreNumber: number) => {
  modelForm.value.validate((valid: boolean) => {
    if (valid) {
      formData.value.approvalOpinion = coreNumber;
      let params: any = { ...formData.value };
      params.id = id.value;
      api.User.blogCheck(params).then((res) => {
        if (res.header.code === 999) {
          if (coreNumber === 1) {
            message.success("文章审批通过！");
          }
          if (coreNumber === 2) {
            message.warning("文章已冻结！");
          }
          if (coreNumber === 3) {
            message.error("文章已驳回！");
          }
          handleCancel();
          emit("refresh", "ok");
        }
      });
    }
  });
};

defineExpose({
  handleShow,
});
</script>

<style lang="less" scoped></style>
